﻿using UnityEngine;
using System.Collections.Generic;
using Pb;
using System.Collections;
/// <summary>
/// 加入房间管理脚本
/// </summary>
public class JoinClubModel : MonoBehaviour
{
    EginUser user = EginUser.Instance;
    public List<UISprite> SpriteNum;
    public List<GameObject> NumberClick;
    List<int> TempNum = new List<int>();
    int showCount;
    public GameObject Num_delete;
    public GameObject Num_Confirm;
    public GameObject CloseBtn;
    public GameObject Num_chongshu;
    public GameObject Send;
    public GameObject Create;
    void Awake()
    {
        for (int i = 0; i < 10; i++)
        {
            UIEventListener.Get(NumberClick[i]).onClick = (ButtonClick);
        }
        UIEventListener.Get(Num_delete).onClick = (ButtonClick);
        UIEventListener.Get(Num_Confirm).onClick = (ButtonClick);
        UIEventListener.Get(CloseBtn).onClick = (ButtonClick);

        UIEventListener.Get(Num_chongshu).onClick = (ButtonClick);
        UIEventListener.Get(Send).onClick = (ButtonClick);
        UIEventListener.Get(Create).onClick = (ButtonClick);
    }
    void ButtonClick(GameObject button)
    {
        if (button.name != "CreateBtn" && button.name != "Num_delete" && button.name != "Num_Confirm" && button.name != "CloseBtn" && button.name != "Num_chongshu" && button.name != "SendBtn")
        {
            NumEvent(int.Parse(button.name));
        }
        else if (button.name == "Num_delete")
        {
            if (showCount > 0)
            {
                TempNum.RemoveAt(TempNum.Count - 1);
                showCount--;
                SpriteNum[showCount].spriteName = " ";
                SpriteNum[showCount].gameObject.SetActive(false);
            }
        }
        else if (button.name == "Num_Confirm")
        {
            if (TempNum.Count == 6)
            {
                string str = "";
                for (int i = 0; i < 6; i++)
                {
                    str = str + TempNum[i].ToString();

                }
                //  StartCoroutine(SendCheckGame(int.Parse(str)));
            }
        }
        else if (button.name == "CloseBtn")
        {
            Debug.Log("..............");
            gameObject.SetActive(false);
        }
        else if (button.name == "Num_chongshu")
        {
            showCount = 0;
            TempNum.Clear();
            for (int i = 0; i < SpriteNum.Count; i++)
            {
                if (SpriteNum[i].gameObject.activeSelf == true)
                {
                    SpriteNum[i].gameObject.SetActive(false);
                }
            }

        }
        else if (button.name == "SendBtn")
        {
            if (showCount == 6)
            {
                //发送申请加入俱乐部消息
                string str = "";
                for (int i = 0; i < 6; i++)
                {
                    str = str + TempNum[i].ToString();

                }
                StartCoroutine(JoinClub(int.Parse(str)));
            }
        }
        else if (button.name == "CreateBtn")
        {
            CreateClub();
        }

    }
    void NumEvent(int num)
    {
        if (TempNum.Count < 6)
        {
            TempNum.Add(num);
            SpriteNum[showCount].spriteName = num.ToString();
            SpriteNum[showCount].GetComponent<UISprite>().MakePixelPerfect();
            SpriteNum[showCount].gameObject.SetActive(true);
            showCount++;
        }
        //if (showCount == 6)
        //{
        //    string str = "";
        //    for (int i = 0; i < 6; i++)
        //    {
        //        str = str + TempNum[i].ToString();

        //    }
        //    //StartCoroutine(SendCheckGame(int.Parse(str)));
        //}
    }

    /// <summary>
    /// 申请加入请求
    /// </summary>
    /// <param name="clubid"></param>
    /// <returns></returns>
    IEnumerator JoinClub(int clubid)
    {
        NnJoinClubRequest req = new NnJoinClubRequest { ClubId = clubid };
        SocketManager sm = SocketManager.Instance;
        sm.SendProtobuf1(req, MsgId.NnJoinClubRequest);
        yield return new WaitForSeconds(0.1f);
    }
    /// <summary>
    /// 创建俱乐部请求
    /// </summary>
    private void CreateClub()
    {
        NnCreateClubRequest req = new NnCreateClubRequest();
        SocketManager sm = SocketManager.Instance;
        sm.SendProtobuf1(req, MsgId.NnCreateClubRequest);
    }
}

